#include<bits/stdc++.h>
using namespace std;
struct Item {
    int weight;
    int value;
    double ratio;
};
bool compareItems(const Item& a, const Item& b){
    return a.ratio > b.ratio;
}
double fractionalKnapsack(int capacity, Item items[], int itemCount){
    double totalValue = 0.0;
    int currentWeight = 0;
    for(int i = 0; i < itemCount; ++i) {
        if(currentWeight + items[i].weight <= capacity){
            totalValue += items[i].value;
            currentWeight += items[i].weight;
        }else {
            int remainingCapacity = capacity - currentWeight;
            totalValue += items[i].ratio * remainingCapacity;
            break;
        }
    }
    return totalValue;
}

int main(){
    int m,n;
    cin >> m >> n;

    Item items[n];

    for (int i = 0; i < n; ++i) {
        cin >> items[i].weight;
    }
    for (int i = 0; i < n; ++i) {
        cin >> items[i].value;
    }
    for (int i = 0; i < n; ++i) {
        items[i].ratio = (double)items[i].value / items[i].weight;
    }

    sort(items, items + n, compareItems);
    cout << "Sorted items by value-to-weight ratio:" << endl;
    for(int i = 0; i < n; ++i) {
        cout << "Item " << i + 1 << ": weight =" << items[i].weight 
        << ", value = " << items[i].value
        << ", ratio = " << items[i].ratio << endl;
    }
    double max_value = fractionalKnapsack(m,items,n);
    cout << "Maximum value in knapsack= " << max_value << endl;
    return 0;
}